package xiao_jin.fantastic_journey.client.renderer;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactory;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.ItemEntity;
import net.minecraft.item.ItemStack;
import org.joml.Quaternionf;
import software.bernie.geckolib.util.RenderUtil;
import xiao_jin.fantastic_journey.core.common.block.entity.relictechblock.RelictechTableBlockEntity;

import java.util.Objects;

@SuppressWarnings("unused")
public class RelictechTableRenderer<T extends RelictechTableBlockEntity> implements BlockEntityRenderer<T>, BlockEntityRendererFactory<T> {
    private ItemEntity item = null;

    public RelictechTableRenderer(BlockEntityRendererFactory.Context context) {
    }

    @Override
    public void render(T tile, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
        if (tile == null) throw new RuntimeException("TileEntity cannot be null!");
        if (item == null)
            item = new ItemEntity(Objects.requireNonNull(tile.getWorld()), tile.getPos().getX(), tile.getPos().getY(), tile.getPos().getZ(), ItemStack.EMPTY);

        matrices.push();
        {
            matrices.translate(0.5, 1.1, 0.5);
            matrices.scale(0.85f, 0.85f, 0.85f);
            item.setStack(tile.inventory.getStack(0));
            MinecraftClient.getInstance().getEntityRenderDispatcher().getRenderer(item)
                    .render(item, (float) RenderUtil.getCurrentTick(), (float) RenderUtil.getCurrentTick(), matrices, vertexConsumers, light);

            matrices.push();
            {
                matrices.translate(-0.350, -0.55, -0.74);
                matrices.multiply(new Quaternionf(1.0f, 0.0f, 0.0f, 22.4f));
                matrices.multiply(new Quaternionf(0.0f, 0.0f, 1.0f, 22f));
                matrices.scale(0.4f, 0.4f, 0.1f);
            }
            matrices.pop();
        }
        matrices.pop();
    }

    @Override
    public BlockEntityRenderer<T> create(Context ctx) {
        return this;
    }
}
